package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.SysDriver;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.ISysDriverService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SysDriverController extends BaseController {

	@Autowired
	private ISysDriverService driverService;
	

	@ResponseBody
	@RequestMapping(value = "/driver", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDeviceAttr(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "";
		String order = "";
		if (!isBlank(req.getParameter("name"))) {
			where +=(where.equals("")? "" : " and ")+ "  name like '%" + req.getParameter("name").trim() + "%'";
		}
		if (!isBlank(req.getParameter("driverType"))) {
			where  +=(where.equals("")? "" : " and ")+  "  driver_type = " + req.getParameter("driverType").trim() ;
		}

		return findList(current, pageSize, where, order, driverService);
	}

	@ResponseBody
	@SysControllerLog(operationName = "添加", modelName = "系统驱动")
	@RequestMapping(value = "/driver", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDeviceAttr(@RequestBody SysDriver record, String  ids, HttpServletRequest req) {
		driverService.insertSelective(record);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "更新", modelName = "系统驱动")
	@RequestMapping(value = "/driver", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDeviceAttr(@RequestBody  SysDriver record, String method, HttpServletRequest req) {
        if(record.getDriverType()==0){
			record.setCommandPort(null);
			record.setBaudRate(null);
			record.setDataBits(null);
			record.setStopBits(null);
			record.setParity(null);
		}else{
			record.setIpport(null);
		}

		driverService.updateByPrimaryKey(record);
		return ApiResult.retOK(req);
	}
	
	@ResponseBody
	@SysControllerLog(operationName = "删除", modelName = "系统驱动")
	@RequestMapping(value = "/driver", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delDeviceAttr(Integer id, HttpServletRequest req) {
//		SysDriver record =new SysDriver();
//		record.setId(id);
//		record.set
		driverService.deleteByPrimaryKey(id);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "系统驱动")
	@RequestMapping(value = "/driver/batchdel", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult batchDelDeviceAttr(String ids, HttpServletRequest req) {

		driverService.deleteByWhere(" id in ("+ids+")");
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@RequestMapping(value = "/driver/list", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findlist(HttpServletRequest req) {
		List<SysDriver> list = driverService.findWhereList("", "");
		return ApiResult.retOK(req, list);
	}


}
